package com.example.administrator.videotest2.util.Tab;

import android.app.ProgressDialog;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.example.administrator.videotest2.R;
import com.example.administrator.videotest2.config.VideoApi;
import com.example.administrator.videotest2.util.Fragment.Tab0_Fragment;
import com.example.administrator.videotest2.util.Fragment.Tab1_Fragment;
import com.example.administrator.videotest2.util.Fragment.Tab2_Fragment;
import com.example.administrator.videotest2.util.Fragment.Tab3_Fragment;
import com.example.administrator.videotest2.util.Fragment.Tab4_Fragment;
import com.example.administrator.videotest2.util.Fragment.Tab5_Fragment;
import com.example.administrator.videotest2.util.GlideImageLoader;
import com.example.administrator.videotest2.util.HttpUtil;
import com.example.administrator.videotest2.util.JsonUtil;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Tab_1Activity extends AppCompatActivity{

    private TabLayout tabLayout;
    private ViewPager viewPager;

    private List<Fragment> list;
    private MyAdapter adapter;
    //private String[] titles = {"推荐","电视剧", "电影", "动漫","日韩影视","福利"};
    private String[] titles = {"推荐","电视剧", "电影", "动漫","日韩影视"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_1);

        //实例化
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        //页面，数据源
        list = new ArrayList<>();
        list.add(new Tab0_Fragment());
        list.add(new Tab1_Fragment());
        list.add(new Tab2_Fragment());
        list.add(new Tab3_Fragment());
        list.add(new Tab4_Fragment());
       // list.add(new Tab5_Fragment());
        //ViewPager的适配器
        adapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        //绑定
        tabLayout.setupWithViewPager(viewPager);
        queryFromServer11(VideoApi.version);


    }


    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        //重写这个方法，将设置每个Tab的标题
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    public void findVersion(String content){
        AlertDialog alertDialog1 = new AlertDialog.Builder(this)
                .setTitle("请更新至最新版本")//标题
                .setMessage(content)//内容
                .setIcon(R.mipmap.ic_launcher)//图标
                .create();
        alertDialog1.show();
    }

    private void queryFromServer11(String address) {


        HttpUtil.sendOKHttpRequest(address, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                Tab_1Activity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Tab_1Activity.this, "加载失败", Toast.LENGTH_SHORT);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String responseText = response.body().string();
                if (Integer.parseInt(responseText)>5) {
                   Tab_1Activity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                           findVersion(responseText);
                        }
                    });
                }
            }
        });
    }

}
